#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import unit_strategy
import param_cache
import os
import logging
import serial


logging.basicConfig(level=logging.DEBUG)


class UnitSerialProcess(unit_strategy.UnitObject):

    def process1(self, paramc: param_cache.ParamCache):
        self.config_reload_serial(paramc.get_param_config())
        self.common = paramc.get_param_serial_com()['common']
        logging.debug("common is :" + self.common)

        # if not self.serial_recv():
        #     raise RuntimeError

    def config_reload_serial(self, con):
        self.timeout = float(con['timeout'])
        self.comparison = con['comparison']
        self.maxrecvlen = int(con['maxrecvlen'])

    def serial_recv(self):
        ser = serial.Serial(self.common, 115200, timeout=self.timeout)
        result = ser.read(self.maxrecvlen).decode('ascii')
        return self.comparison in result